package com.lagou.proxy;

import com.lagou.dao.AccountDao;
import com.lagou.dao.impl.JdbcAccountDaoImpl;
import com.lagou.factory.ProxyFactory;
import com.lagou.pojo.Account;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;

import java.lang.reflect.Method;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：com.lagou.proxy.CglibProxy
 * @作者 mengwei
 * 创建日期：2020/10/15 11:22
 */
public class CglibProxy {

    public static void main(String[] args) throws Exception {
        AccountDao jdbcAccountDao = new JdbcAccountDaoImpl();

        AccountDao proxy = (AccountDao) Enhancer.create(jdbcAccountDao.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

                System.out.println("cglib-before");
                Object invoke = method.invoke(jdbcAccountDao, objects);
                System.out.println("cglib-after");

                return invoke;
            }
        });

        Account account = proxy.queryAccountByCardNo("6029621011000");
        System.out.println(account);    }

        @Test
    public void factoryProxy() throws Exception {
            AccountDao jdbcAccountDao = new JdbcAccountDaoImpl();

            AccountDao cglibProxy = ProxyFactory.newInstance().getCglibProxy(jdbcAccountDao);
            Account account = cglibProxy.queryAccountByCardNo("6029621011000");
            System.out.println(account);
        }
}
